package cc.permission.service;

import cc.cloudapi.permission.proto.user.IntegerEntity;
import cc.cloudapi.permission.proto.user.UserAddEntity;
import cc.cloudapi.permission.proto.user.UserServiceGrpc;
import cc.cloudapi.permission.proto.user.UserUpdateEntity;
import cc.permission.domain.User;
import cc.permission.repository.UserRepo;
import io.grpc.stub.StreamObserver;

/**
 * 
 */
public class UserService extends UserServiceGrpc.UserServiceImplBase{

    private UserRepo userRepo;

    /**
     * Default constructor
     */
    public UserService() {
    }


    /**
     * 注册
     * @return
     */
    public boolean register() {
        // TODO implement here
        return false;
    }

    /**
     * 登录
     * @param userName 用户名
     * @param password 密码
     * @return 用户的菜单
     */
    public String login(String userName, String password) {
        // TODO implement here
        return "";
    }

    /**
     * 新增用户
     * @param request
     * @param responseObserver
     */
    @Override
    public void userAdd(UserAddEntity request, StreamObserver<IntegerEntity> responseObserver){
        /*User user = new User();
        user.setVid(request.getVid());
        user.setLastModifyTime(request.getLastmodifytime());
        user.setStatus(Byte.parseByte(String.valueOf(request.getStatus())));
        user.setPassword(request.getPassword());
        user.setMobile(request.getMobile());
        user.setUserName(request.getUsername());
        userRepo.add(user);
        IntegerEntity builder = IntegerEntity.newBuilder().setIntEnt(1).build();
        responseObserver.onNext(builder);*/
    }

    /**
     * 修改用户
     * @param request
     * @param responseObserver
     */
    @Override
    public void userUpdate(UserUpdateEntity request, StreamObserver<IntegerEntity> responseObserver){
        User user = userRepo.get(request.getNid());
        if (user != null){
            /*user.setVid(request.getVid());
            user.setPassword(request.getPassword());
            user.setUserName(request.getUsername());
            user.setMobile(request.getMobile());
            user.setLastModifyTime(request.getLastmodifytime());
            user.setStatus(Byte.parseByte(String.valueOf(request.getStatus())));*/
            userRepo.update(user);
        }
        IntegerEntity builder = IntegerEntity.newBuilder().setIntEnt(1).build();
        responseObserver.onNext(builder);

    }



}